package com.huitone.gddw.service.impl.worktable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.RepeatRule;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IRepeatRuleDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.IRepeatRuleService;
import com.huitone.gddw.service.dto.RepeatRuleDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 重复短信过滤规则 接口实现类
 * @author wuluodan
 *
 */
@Service
public class RepeatRuleService implements IRepeatRuleService {

	@Autowired
	private IRepeatRuleDao repeatRuleDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<RepeatRuleDTO> selectPage(QueryPage<RepeatRule> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("svrNode", page.getParams().get("svrNode"));
		map.put("serviceCode", page.getParams().get("serviceCode"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<RepeatRuleDTO> result = repeatRuleDao.selectPage(map);
		int totalCount = repeatRuleDao.selectCount(map);
		
		QueryPage<RepeatRuleDTO> queryPage = new QueryPage<RepeatRuleDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public RepeatRule selectByPrimaryKey(String repeatRuleId) {
		return repeatRuleDao.selectByPrimaryKey(repeatRuleId);
	}

	@Override
	public RepeatRuleDTO toUpdate(String repeatRuleId) {
		return new RepeatRuleDTO(this.selectByPrimaryKey(repeatRuleId));
	}

	@Override
	public void changeStatus(RepeatRule entity) {
		this.update(entity);
	}

	@Override
	public void save(RepeatRule entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		repeatRuleDao.save(entity);
		
		this.updateParamRec("保存重复短信过滤规则");
	}

	@Override
	public void update(RepeatRule entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		repeatRuleDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新重复短信过滤规则");
	}

	@Override
	public void delete(String repeatRuleId) {
		repeatRuleDao.deleteByPrimaryKey(repeatRuleId);
		this.updateParamRec("删除重复短信过滤规则");
	}

	@Override
	public List<RepeatRuleDTO> getRecord(Map<String, Object> params) {
		List<RepeatRuleDTO> list = repeatRuleDao.getRecord(params);
		return list;
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.REPEAT_RULE, desc);
	}

}
